import { MockMethod } from 'vite-plugin-mock'

// 模拟角色数据
const roles = [
  {
    id: 1,
    name: '超级管理员',
    description: '系统超级管理员，拥有所有权限',
    created_at: '2023-08-01 10:00:00',
    updated_at: '2023-08-01 10:00:00',
    permission_ids: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
    permission_count: 10
  },
  {
    id: 2,
    name: '普通用户',
    description: '普通用户，拥有基本权限',
    created_at: '2023-08-01 10:00:00',
    updated_at: '2023-08-01 10:00:00',
    permission_ids: [2],
    permission_count: 1
  }
]

export default [
  // 获取角色列表
  {
    url: '/roles',
    method: 'get',
    response: (opt: any) => {
      const { page = 1, page_size = 10, keyword = '' } = opt.query
      
      let result = [...roles]
      
      // 关键词过滤
      if (keyword) {
        const lowerKeyword = keyword.toLowerCase()
        result = result.filter(item => 
          item.name.toLowerCase().includes(lowerKeyword) || 
          item.description.toLowerCase().includes(lowerKeyword)
        )
      }
      
      // 分页
      const total = result.length
      const start = (page - 1) * page_size
      const end = Math.min(start + page_size, total)
      const paginatedResult = result.slice(start, end)
      
      return {
        code: 0,
        data: paginatedResult,
        total,
        errorMsg: ''
      }
    }
  },
  
  // 获取单个角色详情
  {
    url: '/roles/:id',
    method: 'get',
    response: (opt: any) => {
      const { id } = opt.query
      const role = roles.find(r => r.id === Number(id))
      
      if (role) {
        return {
          code: 0,
          data: role,
          errorMsg: ''
        }
      } else {
        return {
          code: 404,
          data: null,
          errorMsg: '角色不存在'
        }
      }
    }
  },
  
  // 创建角色
  {
    url: '/roles',
    method: 'post',
    response: (opt: any) => {
      const { body } = opt
      const newId = Math.max(...roles.map(r => r.id)) + 1
      const now = new Date().toISOString().split('T').join(' ').substring(0, 19)
      
      const permission_ids = body.permission_ids || []
      
      const newRole = {
        id: newId,
        name: body.name,
        description: body.description,
        permission_ids,
        permission_count: permission_ids.length,
        created_at: now,
        updated_at: now
      }
      
      roles.push(newRole)
      
      return {
        code: 0,
        data: newRole,
        errorMsg: ''
      }
    }
  },
  
  // 更新角色
  {
    url: '/roles/:id',
    method: 'put',
    response: (opt: any) => {
      const { id } = opt.query
      const { body } = opt
      const index = roles.findIndex(r => r.id === Number(id))
      
      if (index !== -1) {
        const now = new Date().toISOString().split('T').join(' ').substring(0, 19)
        const updatedRole = {
          ...roles[index],
          name: body.name || roles[index].name,
          description: body.description || roles[index].description,
          updated_at: now
        }
        
        roles[index] = updatedRole
        
        return {
          code: 0,
          data: updatedRole,
          errorMsg: ''
        }
      } else {
        return {
          code: 404,
          data: null,
          errorMsg: '角色不存在'
        }
      }
    }
  },
  
  // 删除角色
  {
    url: '/roles/:id',
    method: 'delete',
    response: (opt: any) => {
      const { id } = opt.query
      const index = roles.findIndex(r => r.id === Number(id))
      
      if (index !== -1) {
        roles.splice(index, 1)
        
        return {
          code: 0,
          data: null,
          errorMsg: ''
        }
      } else {
        return {
          code: 404,
          data: null,
          errorMsg: '角色不存在'
        }
      }
    }
  },
  
  // 分配权限
  {
    url: '/roles/:id/assign-permissions',
    method: 'post',
    response: (opt: any) => {
      const { id } = opt.query
      const { body } = opt
      const index = roles.findIndex(r => r.id === Number(id))
      
      if (index !== -1) {
        const now = new Date().toISOString().split('T').join(' ').substring(0, 19)
        const permission_ids = body.permission_ids || []
        
        roles[index] = {
          ...roles[index],
          permission_ids,
          permission_count: permission_ids.length,
          updated_at: now
        }
        
        return {
          code: 0,
          data: null,
          errorMsg: ''
        }
      } else {
        return {
          code: 404,
          data: null,
          errorMsg: '角色不存在'
        }
      }
    }
  }
] as MockMethod[]
